package com.qfedu.buy.common.util;

import com.qfedu.buy.common.config.SystemConfig;
import org.redisson.Redisson;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @program: StartCoding
 * @description:
 * @author: Feri(邢朋辉)
 * @create: 2021-01-06 14:48
 */
public class RedissonUtil {

    //实例化
    public static RedissonClient client;
    static {
        Config config=new Config();
        config.useSingleServer().
                setAddress("redis://"+ SystemConfig.REDIS_HOST+":"+SystemConfig.REDIS_PORT).
                setPassword(SystemConfig.REDIS_PASS);
        client=Redisson.create(config);
    }
    //新增
    public static void setStr(String key,Object val,int seconds){
        client.getBucket(key).set(val,seconds, TimeUnit.SECONDS);
    }
    public static void setSet(String key,String val){
        client.getSet(key).add(val);
    }
    public static void setStr(String key,Object val){
        client.getBucket(key).set(val);
    }
    public static void setHash(String key,String field,String val){
        client.getMap(key).put(field,val);
    }
    public static void setHash(String key,String field,Object val){
        client.getMap(key).put(field,val);
    }
    public static <T> void  setList(String key, List<T> list){
        client.getList(key).addAll(list);
    }
    //删除
    public static void delKeys(String... keys){
        client.getKeys().delete(keys);
    }
    public static void delField(String key,String field){
        client.getMap(key).remove(field);
    }
    //查询
    public static String getStr(String key){
        return client.getBucket(key).get().toString();
    }
    public static Object getObj(String key){
        return client.getBucket(key).get();
    }
    public static String getHash(String key,String field){
        return client.getMap(key).get(field).toString();
    }
    public static Object getHashObj(String key,String field){
        return client.getMap(key).get(field);
    }
    public static List getList(String key){
        return client.getList(key).readAll();
    }
    public static int getLiseSize(String key){
        return client.getList(key).size();
    }
    public static Object getList(String key,int index){
        return client.getList(key).get(index);
    }
    public static boolean checkField(String key,String field){
        return client.getMap(key).containsKey(field);
    }
    public static boolean checkSet(String key,String val){
        return client.getSet(key).contains(val);
    }
    public static boolean isHave(String key){
        return client.getKeys().countExists(key)>0;
    }

    //系统
    public static void expire(String key,int seconds){
        client.getKeys().expire(key,seconds,TimeUnit.SECONDS);
    }
    public static Iterable<String> keys(String preKey){
        return client.getKeys().getKeysByPattern(preKey);
    }

    //布隆过滤
    public static void filter(){
        //创建布隆过滤器
        RBloomFilter<String> filter=client.getBloomFilter("names");
        //完成初始化 设置参数：1.元素个数 2.误差率
        filter.tryInit(100000000L,0.01);
        //遍历 添加元素
        filter.add("老邢");
        //校验
        System.err.println(filter.contains("老邢"));
        System.err.println(filter.contains("老邢123"));

    }
    //分布式锁 加锁
    public static void lock(String key,int mseconds){
        client.getLock(key).lock(mseconds,TimeUnit.MILLISECONDS);
    }
    public static RLock getLock(String key, int seconds){
        RLock rLock=client.getLock(key);
        rLock.lock(seconds,TimeUnit.SECONDS);
        return rLock;
    }
    //释放锁
    public static void unlock(String key){
        client.getLock(key).unlock();
    }
}
